package com.elective.courseselect.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.elective.courseselect.dao.CommentingDao;
import com.elective.courseselect.entity.CommentEntity;
import com.elective.courseselect.entity.CommentingEntity;
import com.elective.courseselect.entity.CourseEntity;
import com.elective.courseselect.service.CommentService;
import com.elective.courseselect.service.CommentingService;
import com.elective.courseselect.service.CourseService;
import com.elective.courseselect.serviceUtils.UserUtils;
import com.elective.courseselect.utils.ListUtils;
import com.elective.courseselect.utils.PageUtils;
import com.elective.courseselect.utils.Query;
import com.elective.courseselect.utils.R;
import com.elective.status.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;


@Service("commentingService")
@Transactional(rollbackFor = Exception.class)
public class CommentingServiceImpl extends ServiceImpl<CommentingDao, CommentingEntity> implements CommentingService {

    @Autowired
    CourseService courseService;

    @Autowired
    CommentService commentService;


    @Override

    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CommentingEntity> page = this.page(
                new Query<CommentingEntity>().getPage(params),
                new QueryWrapper<CommentingEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public R commentsClass(CommentingEntity commentingEntity) {
        QueryWrapper<CommentingEntity> commentingEntityQueryWrapper = new QueryWrapper<>();
        commentingEntityQueryWrapper.eq("student_id", commentingEntity.getStudentId());
        commentingEntityQueryWrapper.eq("course_id", commentingEntity.getCourseId());
        CourseEntity course = courseService.getById(commentingEntity.getCourseId());
        Boolean abandon = UserUtils.isAbandon(course.getTeacherId());
        if (abandon) {
            return R.error(StatusCode.ERROR, "老师账号以弃用,无法评论");
        } else {
            this.saveOrUpdate(commentingEntity, commentingEntityQueryWrapper);
            return R.ok(StatusCode.OK, "评论课程成功");
        }
    }

    @Override
    public R searchCourseComment(Integer courseId) {
        QueryWrapper<CommentingEntity> coursecommenting = new QueryWrapper<>();
        coursecommenting.eq("course_id", courseId);
        List<CommentingEntity> list = this.list(coursecommenting);
        if (CollectionUtils.isEmpty(list)) {
            return R.error(StatusCode.ERROR, "此门课程没有评论");
        }


        JSONObject no1 = new JSONObject();
        for (CommentingEntity commentingEntity : list) {
            JSONArray no2 = new JSONArray();
            JSONObject no3 = new JSONObject();
            String commentId = commentingEntity.getCommentId();
            if (!StringUtils.isEmpty(commentId)) {
                for (String result : commentId.split(",")) {
                    JSONObject no5 = new JSONObject();
                    JSONArray no4 = new JSONArray();
                    CommentEntity commentEntity = commentService.getById(Integer.valueOf(result));
                    if (commentEntity == null) {
                        no5.put("commentId", "该标签已被管理员删除");
                        no5.put("commentLable", "该标签已被管理员删除");
                    } else {
                        no5.put("commentId", commentEntity.getId());
                        no5.put("commentLable", commentEntity.getLabel());
                    }
                    no4.add(no5);
                    if (no3.containsKey("commenting") && null !=no3.getJSONArray("commenting")) {
                        no3.getJSONArray("commenting").addAll(no4);
                    }else{
                        no3.put("commenting", no4);
                    }
                }
                no3.put("stuId", commentingEntity.getStudentId());
            }
            no2.add(no3);
            if (no1.containsKey("stuIdGroup") && null !=no1.getJSONArray("stuIdGroup")) {
                no1.getJSONArray("stuIdGroup").addAll(no2);
            }else{
                no1.put("stuIdGroup", no2);
            }
        }
        no1.put("courseId", courseId);

        return R.ok(StatusCode.OK, "查询课程评论成功").put("data", no1);
    }

    @Override
    public R delStuCommenting(Integer courseId , Integer stuId , Integer[] commentId) {
        QueryWrapper<CommentingEntity> commentingEntityQueryWrapper = new QueryWrapper<>();
        commentingEntityQueryWrapper.eq("student_id", stuId);
        commentingEntityQueryWrapper.eq("course_id",  courseId);

        CommentingEntity one = this.getOne(commentingEntityQueryWrapper);
        String commentIds = one.getCommentId();
        if (StringUtils.isEmpty(commentIds)) {
            return R.error(StatusCode.ERROR, "此门课程并无评论");
        }else{
            List< String> arrayList = new ArrayList<String>(commentIds.split(",").length);
            Collections.addAll(arrayList, commentIds.split(","));
            for (Integer integer : commentId) {
                for (int i = 0; i < arrayList.size(); i++) {
                    if (integer == Integer.valueOf(arrayList.get(i))) {
                        arrayList.remove(i);
                        //去除两端括号
                        String strip = ListUtils.strip(arrayList.toString());
                        one.setCommentId(strip);
                        break;
                    }
                }
                this.updateById(one);
            }
            return R.ok(StatusCode.OK, "删除学生评论成功");
        }
    }
}